﻿using MailKit.Net.Smtp;
using MimeKit;

namespace FastEasy.Common.Mail
{
    public static class EmailKit
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mime">邮件消息体</param>
        /// <returns></returns>
        public static async Task<bool> SendMail(Mime mime)
        {
            //邮件实例
            MimeMessage message = new()
            {
                Subject = mime.Subject,
                Body = mime.Body,
            };
            message.To.Add(mime.To);
            message.From.Add(mime.From);
            try
            {
                using var client = new SmtpClient();
                client.Connect("smtp.163.com", mime.Port, mime.Port == 465);
                client.Authenticate(mime.From.Address, mime.Code);
                await client.SendAsync(message);
                client.Disconnect(true);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}